#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class NoteProvider : CY.GFive.Core.Providers.DALProvider.INoteProvider
	{
		#region Sql
		private static readonly string SqlSelectNote = "SELECT [Id], [NoteTitle],[NoteContent],[NoteDate],[NoteTimes],[NoteType],[PulisherKey],[PulishModule] FROM [Note] WHERE  [Id]=@Id";

		private static readonly string SqlInsertNote = "INSERT INTO [Note]([NoteTitle],[NoteContent],[NoteDate],[NoteTimes],[NoteType],[PulisherKey],[PulishModule]) values(@NoteTitle,@NoteContent,@NoteDate,@NoteTimes,@NoteType,@PulisherKey,@PulishModule);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateNote = "UPDATE [Note] SET [NoteTitle] = @NoteTitle,[NoteContent] = @NoteContent,[NoteDate] = @NoteDate,[NoteTimes] = @NoteTimes,[NoteType] = @NoteType,[PulisherKey] = @PulisherKey,[PulishModule] = @PulishModule WHERE  [Id]=@Id";

		private static readonly string SqlDeleteNote = "DELETE [Note] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllNote = "SELECT [Id], [NoteTitle],[NoteContent],[NoteDate],[NoteTimes],[NoteType],[PulisherKey],[PulishModule] FROM [Note]";
		#endregion

		#region INoteProvider Members
		public Core.Business.Note Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectNote);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.Note note = new Core.Business.Note();

				if (!reader.IsDBNull(0)) note.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) note.NoteTitle = reader.GetString(1);
				if (!reader.IsDBNull(2)) note.NoteContent = reader.GetString(2);
				if (!reader.IsDBNull(3)) note.NoteDate = reader.GetDateTime(3);
				if (!reader.IsDBNull(4)) note.NoteTimes = reader.GetInt32(4);
				if (!reader.IsDBNull(5)) note.NoteType = reader.GetInt32(5);
				if (!reader.IsDBNull(6)) note.PulisherKey = reader.GetInt32(6);
				if (!reader.IsDBNull(7)) note.PulishModule = reader.GetInt32(7);

				return note;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.Note note)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@NoteTitle", SqlDbType.NVarChar, note.NoteTitle);
			sql.AddParameter("@NoteContent", SqlDbType.NVarChar, note.NoteContent);
			sql.AddParameter("@NoteDate", SqlDbType.DateTime, note.NoteDate);
			sql.AddParameter("@NoteTimes", SqlDbType.Int, note.NoteTimes);
			sql.AddParameter("@NoteType", SqlDbType.Int, note.NoteType);
			sql.AddParameter("@PulisherKey", SqlDbType.Int, note.PulisherKey);
			sql.AddParameter("@PulishModule", SqlDbType.Int, note.PulishModule);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertNote);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) note.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.Note note)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@NoteTitle", SqlDbType.NVarChar, note.NoteTitle);
			sql.AddParameter("@NoteContent", SqlDbType.NVarChar, note.NoteContent);
			sql.AddParameter("@NoteDate", SqlDbType.DateTime, note.NoteDate);
			sql.AddParameter("@NoteTimes", SqlDbType.Int, note.NoteTimes);
			sql.AddParameter("@NoteType", SqlDbType.Int, note.NoteType);
			sql.AddParameter("@PulisherKey", SqlDbType.Int, note.PulisherKey);
			sql.AddParameter("@PulishModule", SqlDbType.Int, note.PulishModule);
			sql.AddParameter("@Id", SqlDbType.Int, note.Id);
			sql.ExecuteSql(SqlUpdateNote);
		}

		public void Delete(Core.Business.Note note)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, note.Id);
			sql.ExecuteSql(SqlDeleteNote);
		}

		public IList<Core.Business.Note> GetAllNote()
		{
			IList<Core.Business.Note> notelist = new List<Core.Business.Note>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllNote);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.Note note = new Core.Business.Note();

					if (!reader.IsDBNull(0)) note.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) note.NoteTitle = reader.GetString(1);
					if (!reader.IsDBNull(2)) note.NoteContent = reader.GetString(2);
					if (!reader.IsDBNull(3)) note.NoteDate = reader.GetDateTime(3);
					if (!reader.IsDBNull(4)) note.NoteTimes = reader.GetInt32(4);
					if (!reader.IsDBNull(5)) note.NoteType = reader.GetInt32(5);
					if (!reader.IsDBNull(6)) note.PulisherKey = reader.GetInt32(6);
					if (!reader.IsDBNull(7)) note.PulishModule = reader.GetInt32(7);

					note.MarkOld();
					notelist.Add(note);
				}
				reader.Close();
			}
			return notelist;
		}
		#endregion
	}
}
